import Request from 'utils/request';
import SystemUtil from 'utils/systemUtil';
import moment from 'moment';
import Storage from 'storage';

import GoodsPaymentApi from 'services/goodsPaymentApi';

import GroupBargainSearch from 'models/groupBargain/groupBargainSearch';
import GroupBargainGoods from 'models/groupBargain/groupBargainGoods';
import GoodsGroupDetail from 'models/groupBargain/goodsGroupDetail';
import GoodsBargainDetail from 'models/groupBargain/goodsBargainDetail';
import BargainSituationData from 'models/groupBargain/bargainSituationData';

export default class GroupBargainApi {
  static getGroupBargainTimes() {
    const apiName = 'api/wehcatshop/activity/bargainingAndGroupTimes';

    return Request.post(apiName).then(res => {
      let search = new GroupBargainSearch(res.data);
      let systemTime = res.systemTime;
      return {
        search,
        systemTime
      };
    });
  }

  static getGroupBargainGoodsList(search, pagination) {
    const apiName = 'api/wehcatshop/activity/bargainingAndGroupGoodsList';

    let curTimes = search.curTimes;
    let postData = {
      activityStartTime: moment(curTimes.startTime).format('YYYY-MM-DD HH:mm:ss'),
      activityEndTime: moment(curTimes.endTime).format('YYYY-MM-DD HH:mm:ss'),
      limit: pagination.pageSize,
      page: pagination.currentPage
    };

    return Request.post(apiName, postData).then(res => {
      let goodsList = res.data;
      let goodsData = [];
      for (let i = 0; i < goodsList.length; i++) {
        let goods = goodsList[i];
        goods = new GroupBargainGoods(goods);
        goodsData.push(goods);
      }

      pagination.totalCount = res.rows;
      return goodsData;
    });
  }

  static getGoodsGroupDetail(id) {
    const apiName = 'api/wehcatshop/activity/groupGoodsInfo';

    return Request.post(apiName, {
      goodsDefId: id
    }).then(res => {
      let detail = res.data;
      let systemTime = res.systemTime;
      detail = new GoodsGroupDetail(detail, systemTime);
      return {
        detail,
        systemTime
      };
    });
  }

  static async getRecommendGroup(goodsGroup, unionid) {
    if (unionid === undefined) {
      return this.getRecommendGroupData(goodsGroup);
    }

    let groupData = await this.getGroupById(goodsGroup.defId, unionid);
    return this.getRecommendGroupData(goodsGroup, 1).then(data => {
      data = [groupData, ...data];
      return data;
    });
  }

  static getRecommendGroupData(goodsGroup, limit = 2) {
    const apiName = 'api/wehcatshop/activity/recommendGroupInfo';

    return Request.post(apiName, {
      goodsDefId: goodsGroup.defId,
      limit: limit
    }).then(res => {
      return res.data;
    });
  }

  static getGroupById(defId, unionid) {
    const apiName = 'api/wehcatshop/activity/selfGroupInfo';
    let postData = {
      goodsDefId: defId
    };
    if (SystemUtil.isEmpty(unionid)) {
      postData.unionid = unionid;
    }

    return Request.post(apiName, postData).then(res => {
      return res.data;
    });
  }

  static toPayJoinGroup(goodsGroup, group, lnglat) {
    let sponsorUnionid = group.sponsorUnionid;
    if (SystemUtil.isEmpty(sponsorUnionid)) {
      let user = Storage.getSession('user');
      sponsorUnionid = user.unionid;
    }
    let data = goodsGroup.getPayObject();
    data.attach = sponsorUnionid;
    return GoodsPaymentApi.payActivityGoodsList(data, lnglat, 'PT').then(orderId=>{
      return {
        sponsorUnionid,
        orderId
      };
    });
  }

  static getGoodsBargainDetail(id) {
    const apiName = 'api/wehcatshop/activity/bargainingAndGroupGoodsInfo';

    return Request.post(apiName, {
      goodsDefId: id
    }).then(res => {
      let detail = res.data;
      let systemTime = res.systemTime;
      detail = new GoodsBargainDetail(detail, systemTime);
      return {
        detail,
        systemTime
      };
    });
  }

  static getGoodsBargainSituation(id, unionid, hasObj = true) {
    const apiName = 'api/wehcatshop/activity/bargaining';

    let postData = {
      goodsDefId: id
    };

    if (unionid !== null) {
      postData.sponsorUnionid = unionid;
    }

    return Request.post(apiName, postData).then(res => {
      let data = res.data;
      if (!hasObj) {
        return data;
      }
      let systemTime = res.systemTime;
      data = new BargainSituationData(data, systemTime, unionid);
      return {
        data,
        systemTime
      };
    });
  }

  static getGoodsBargainRecordList(bargainData, isPrice = false) {
    let promiseList = [this.getGoodsBargainHistory(bargainData)];

    if (isPrice) {
      promiseList.push(this.getGoodsBargainSituation(bargainData.defId, bargainData.unionid, false));
    }

    return window.Promise.all(promiseList).then(res => {
      let data = {
        recordList: res[0]
      };
      if (isPrice) {
        data.goods = res[1];
      }
      return data;
    });
  }

  static getGoodsBargainHistory(bargainData) {
    const apiName = 'api/wehcatshop/activity/bargainingList';

    let postData = {
      goodsDefId: bargainData.defId,
      activityLogId: bargainData.maxRecord
    };
    let unionid = bargainData.unionid;
    if (unionid !== null) {
      postData.sponsorUnionid = unionid;
    }
    return Request.post(apiName, postData).then(res => {
      return res.data;
    });
  }

  static bargainGoods(bargain) {
    const apiName = 'api/wehcatshop/activity/bargainingAdd';

    let postData = {
      goodsDefId: bargain.defId,
      goodsModelId: bargain.modelId
    };

    let unionid = bargain.unionid;
    if (unionid !== null) {
      postData.sponsorUnionid = unionid;
    }

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static toPayBargainGoods(bargain, lnglat) {
    let data = bargain.getPayObject();
    return GoodsPaymentApi.payActivityGoodsList(data, lnglat, 'KJ');
  }
}
